package com.comiee.tankwar;

import java.util.List;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
/**
 * 子弹类
 * @author comiee
 *
 */
public class Missile extends Sprite {
	private float speed=20;	//移动速度
	private int health=0;	//生命，随时间减少，为0时死亡
	boolean live=true;	//是否存活
	Tank tank;	//属于哪辆坦克
	
	public Missile(Tank tank) {
		super(new Texture("images/missile.png"));
		this.tank = tank;
		this.health=30;
		float x,y;
		x = tank.getX()+tank.getOriginX();	//坦克中心
		y = tank.getY()+tank.getOriginY();
		double r = (tank.getRotation()+90)*Math.PI/180;
		x += tank.getOriginY()*Math.cos(r)*0.9;	//炮口位置
		y += tank.getOriginY()*Math.sin(r)*0.9;
		x += super.getOriginY()*Math.cos(r);	//子弹中心
		y += super.getOriginY()*Math.sin(r);
		x -= super.getOriginX();				//子弹坐标
		y -= super.getOriginY();
		super.setPosition(x, y);
		super.setRotation(tank.getRotation());
	}
	
	public void move() {
		double r = (super.getRotation()+90)*Math.PI/180;
		super.translate((float)(speed*Math.cos(r)), (float)(speed*Math.sin(r)));
	}

	public boolean test(List<Tank> enemy, Terrain terrain, List<Sprite> all) {
		if (health==0) return false;
		else health--; 
		for (Tank i : enemy) {
			if (this.getBoundingRectangle().overlaps(i.getBoundingRectangle())) {
				tank.hite(i);	//伤害敌人
				return false;
			}
		}
		for (Sprite i : terrain.list1) {
			if (this.getBoundingRectangle().overlaps(i.getBoundingRectangle())) {
				return false;	//撞墙消失
			}
		}
		for (Sprite i : terrain.list2) {
			if (this.getBoundingRectangle().overlaps(i.getBoundingRectangle())) {
				terrain.list2.remove(i);	//破坏墙壁
				all.remove(i);
				return false;
			}
		}
		return true;
	}
}
